Fix an incorrect merge of credentials. Add a new test for that.
authorEvgen Druzhynin <evgen.druzhynin@gmail.com>
Wed, 21 Jun 2017 16:48:40 +0000 (19:48 +0300)
committerEvgen Druzhynin <evgen.druzhynin@gmail.com>
Wed, 21 Jun 2017 16:48:40 +0000 (19:48 +0300)
src/cargo/util/config.rs
tests/login.rs

index 5edeeef96374971692401e6d2cf99ac58e77bf61..2df174740df483a0109c7305201225a8eae1afe2 100644 (file)
@@ -486,13 +486,18 @@ impl Config {
             _ => unreachable!(),
         };
 
-        let mut registry = cfg.entry("registry".into())
-            .or_insert(CV::Table(HashMap::new(),
-                                 PathBuf::from(".")));
-        registry.merge(value).chain_err(|| {
-            format!("failed to merge configuration at `{}`",
-                          credentials.display())
-        })?;
+        let registry = cfg.entry("registry".into())
+                          .or_insert(CV::Table(HashMap::new(), PathBuf::from(".")));
+
+        match (registry, value) {
+            (&mut CV::Table(ref mut old, _), CV::Table(ref mut new, _)) => {
+                let new = mem::replace(new, HashMap::new());
+                for (key, value) in new.into_iter() {
+                    old.insert(key, value);
+                }
+            }
+            _ => unreachable!(),
+        }
 
         Ok(())
     }
index 306b4e0d9ea6c200afe2145dc314da5922830395..d3b9b601b2feb8327a1a8228976472ce295e348c 100644 (file)
@@ -11,6 +11,8 @@ use cargotest::cargo_process;
 use cargotest::support::execs;
 use cargotest::support::registry::registry;
 use cargotest::install::cargo_home;
+use cargo::util::config::Config;
+use cargo::core::Shell;
 use hamcrest::{assert_that, existing_file, is_not};
 
 const TOKEN: &str = "test-token";
@@ -110,3 +112,17 @@ fn login_without_credentials() {
     File::open(&credentials).unwrap().read_to_string(&mut contents).unwrap();
     assert!(check_host_token(contents.parse().unwrap()));
 }
+
+#[test]
+fn new_credentials_is_used_instead_old() {
+    setup_old_credentials();
+    setup_new_credentials();
+
+    assert_that(cargo_process().arg("login")
+                .arg("--host").arg(registry().to_string()).arg(TOKEN),
+                execs().with_status(0));
+
+    let config = Config::new(Shell::new(), cargo_home(), cargo_home());
+    let token = config.get_string("registry.token").unwrap().map(|p| p.val);
+    assert!(token.unwrap() == TOKEN);
+}